<template>
	<view>
		<uv-navbar :title="title" :placeholder="true" :autoBack="true" bgColor="rgba(0,0,0,0)"></uv-navbar>
		<view class="headBg textC" v-if="type=='change'">
			<view class="font-32">累计转店<text class="font-64 font-weight f-t">{{priceA||0}}</text>家</view>
			<view class="font-32">留下你的电话 快速转店 快至<text class="font-64 font-weight f-t">{{priceB||0}}</text>天</view>
		</view>
		<view class="headBg textC" v-if="type=='look'">
			<view class="font-32">累计找店<text class="font-64 font-weight f-t">{{priceA||0}}</text>家</view>
			<view class="font-32"><text class="font-64 font-weight f-t">{{priceB||0}}</text>+店铺资源 快速找店选址</view>
		</view>

		<input maxlength="11" type="tel" v-model="form.mobile" class="inputBox" placeholder="请输入手机号码" placeholder-class="textC">

		<view class="m-t-60 p-30 btnBox">
			
			<button type="primary" @click="submit(1)">{{type=='look'?'人工客服一对一精准对接':'全网推广'}}</button>
			<button type="primary" @click="submit(0)">免费提交</button>
		</view>

		<uv-popup ref="popupPLone" round="8" :safeAreaInsetBottom="false" :closeable="true">
			<view class="payBox">
				<view class="font-36 font-weight">支付金额(元)</view>
				<view class="font-60 font-weight f-t m-t-40">{{shop_price||0}}元</view>
				<view class="m-b-30 m-t-30" style="width: 100%;height: 2rpx;background-color: #efefef;"></view>
				<button type="primary" @click="paySubmit">支付定金</button>
				<view class="flex m-t-30" @click="payShow=!payShow">
					<view class="iconfont icon-choose f-t" v-if="payShow" style="font-size: 40rpx;"></view>
					<view class="iconfont icon-weixuanzhong" v-else style="font-size: 40rpx;"></view>
					<view class="">我已阅读并同意<text style="color: #385373;" @click.stop="xieYi" >《支付协议》</text></view>
				</view>
			</view>
		</uv-popup>
		
		<uv-popup ref="popupPay" round="8" :safeAreaInsetBottom="false" :closeable="true">
			<scroll-view scroll-y="true" class="payBox">
				<view v-html="pay_agreement">
				</view>
			</scroll-view>
		</uv-popup>
		
		<uv-toast ref="uToast"></uv-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				payShow:false,
				form: {
					mobile: '',
					order_type: '',//0转店,1找店
					coupon_id: '0',//优惠券
				},
				shop_price:'',
				order_no:'',
				pay_agreement:'',
				type:'',
				title:'',
				priceA:'0',
				priceB:'0'
			};
		},
		onLoad(res) {
			this.config(res.type)
			if(res.type=='look') {
				this.title = '快速找店'
				this.form.order_type = 1
			}else {
				this.title = '快速转店'
				this.form.order_type = 0
			}
			this.type = res.type
		},
		methods: {
			
			xieYi() {
				this.$refs.popupPay.open('center');
			},
			paySubmit() {
				if(!this.payShow) {
					uni.showToast({
						title:'请先勾选协议',
						icon:'error'
					})
					return
				}
				this.$api.sendRequest({
					url:'/api/store/storepay',
					data:{
						order_no:this.order_no
					},success:res=>{
						let data = res.data
						if(res.code==1) {
							uni.requestPayment({
								"provider": "wxpay",
								timeStamp: data.timeStamp, //后端返回的时间戳
								nonceStr: data.nonceStr, //后端返回的随机字符串
								package: data.package, //后端返回的prepay_id
								signType: data.signType, //后端签名算法,根据后端来,后端MD5这里即为MD5
								paySign: data.paySign, //后端返回的签名
								success: res => {
									uni.showModal({
										title: '提示',
										content: '支付成功',
										cancelText: '确定',
										showCancel: false,
										success: resA => {
											uni.navigateBack()
										}
									});
								},
								fail(e) {
									uni.showToast({
										title: '取消支付',
										icon: 'error'
									})
								}
							})
						}else {
							this.$refs.uToast.show({
								type: 'default',
								message: res.msg,
								duration:1500
							})
						}
						
					}
				})
			},
			submit(type) {
				
				if(!this.form.mobile) {
					uni.showToast({
						title:'请填写手机号',
						icon:'error'
					})
					return
				}
				this.$api.sendRequest({
					url: '/api/store/createStore',
					data: {...this.form,type:type},
					success: res => {
						if(res.code==1) {
							if(res.data.pay_status=='0') {
								this.order_no = res.data.order_no
								this.$refs.popupPLone.open('center');
							}else {
								this.$refs.uToast.show({
									type: 'default',
									message: '提交成功',
									duration:1500
								})	
								setTimeout(()=>{
									uni.navigateBack()
								},1500)
							}
						}else {
							this.$refs.uToast.show({
								type: 'default',
								message: res.msg,
								duration:1500
							})
						}
					}
				})
			},
			config(type) {
				// "quick_shop_search_price": "1", //快速找店推广金额
				// "quick_change_shop_price": "1", //快速转店推广金额
				this.$api.sendRequest({
					url:'/api/index/config',
					data:{},success:res=>{
						if(type=='change') {
							this.shop_price = res.data.quick_change_shop_price
							this.priceA = res.data.change_store_count
							this.priceB = res.data.change_store_day
						}else {
							this.shop_price = res.data.quick_shop_search_price
							
							this.priceA = res.data.find_store_count
							this.priceB = res.data.store_count
						}
						this.pay_agreement = res.data.pay_agreement
						
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.payBox {
		width: 514rpx;
		height: 370rpx;
		border-radius: 16rpx;
		background-color: #fff;
		padding: 30rpx 25rpx;
	}

	.headBg {
		align-items: flex-start;
		width: 750rpx;
		height: 304rpx;
		background: linear-gradient(180deg, (#fff 0%, #f1f6fe 20%, #e9f2ff 80%, #fafcff 100%));
		padding-top: 30rpx;

	}

	.inputBox {
		width: 345px;
		height: 48px;
		border-radius: 4px;
		opacity: 1;
		background: #F1F4F8;
		margin: 0 auto;
		text-align: center;
	}

	.btnBox {
		:nth-child(1) {
			margin-bottom: 30rpx !important;
		}

		:nth-child(2) {
			border: 2rpx solid rgba(227, 231, 236, 1.0);
			background-color: #fff;
			color: rgba(146, 153, 163, 1);
		}
	}
</style>
